package com.memory.test.comment.util;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * @author zwy
 */
public class FanyiBaiduUtil {
    private static String appId = "";
    private static String salt = "";
    private static String key = "";
    private static String sign = "";
    private static String url = "http://api.fanyi.baidu.com/api/trans/vip/translate";
    private static SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");//设置日期格式

    public static void main(String[] args) throws JSONException {
        FanyiBaiduUtil test = new FanyiBaiduUtil();
//        translate("apple");
    }

    public static String translateEC(String word) throws JSONException {

        salt = df.format(new Date());
        //salt=new Date().toString();
        sign = MD5Utils.code(appId + word + salt + key);
        url = "http://api.fanyi.baidu.com/api/trans/vip/translate?q=" + word + "&from=en&to=zh&appid=" + appId + "&salt=" + salt + "&sign=" + sign;
        String s = httpRequest(url);
        String s1 = parseJSONWithJSONObject(s);
        return s1;
    }
    public static String translateCE(String word) throws JSONException {

        salt = df.format(new Date());
        //salt=new Date().toString();
        sign = MD5Utils.code(appId + word + salt + key);
        url = "http://api.fanyi.baidu.com/api/trans/vip/translate?q=" + word + "&from=zh&to=en&appid=" + appId + "&salt=" + salt + "&sign=" + sign;
        String s = httpRequest(url);
        String s1 = parseJSONWithJSONObject(s);
        return s1;
    }

    private static String httpRequest(String requestUrl) {
        //buffer用于接受返回的字符
        StringBuffer buffer = new StringBuffer();
        try {
            //建立URL，把请求地址给补全，其中urlencode（）方法用于把params里的参数给取出来
            URL url = new URL(requestUrl);
            //打开http连接
            HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection();
            httpUrlConn.setDoInput(true);
            httpUrlConn.setRequestMethod("GET");
            httpUrlConn.connect();

            //获得输入
            InputStream inputStream = httpUrlConn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

            //将bufferReader的值给放到buffer里
            String str = null;
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            //关闭bufferReader和输入流
            bufferedReader.close();
            inputStreamReader.close();
            inputStream.close();
            inputStream = null;
            //断开连接
            httpUrlConn.disconnect();

        } catch (Exception e) {
            e.printStackTrace();
        }
        //返回字符串
        return buffer.toString();
    }

    private static String parseJSONWithJSONObject(String JsonData) throws JSONException {
        JSONObject json = new JSONObject(JsonData);
       // JSONObject jsonObj = json.getJSONObject("trans_result");//获取json数组中的data项
        JSONArray paths =
        json.getJSONArray("trans_result");
        String res = paths.getJSONObject(0).getString("dst");
       return res;
    }
}
